﻿
Partial Class WebForm_ManageRegion_SearchRegion
    Inherits My.MyUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            RaiseEvent Loaded(Me, New EventArgs)
        End If
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        If Me.DelData(e.RowIndex) = True Then
            Me.BindGV()
        End If

    End Sub

    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Dim key As DataKey = Me.gv.DataKeys(e.RowIndex)
        Me.SelectedRegionID = key("RegionID")
        RaiseEvent Updating(Me, New EventArgs)
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Me.DefaultParentID = Me.txtParent.Text
        RaiseEvent Adding(Me, New EventArgs)
    End Sub
End Class

Partial Class WebForm_ManageRegion_SearchRegion
    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select r.* ")
        sql.AppendFormat(" from Region r ")
        sql.AppendFormat("inner join Region r_parent on r.Parent = r_Parent.RegionID ")
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat(Me.GetWhereSQL())

        dt = dat.GetData(sql)
        Me.gv.DataSource = dt
        Me.gv.DataBind()
    End Sub

    Public Sub BindGV(ByVal regionID As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select r.* ")
        sql.AppendFormat(" from Region r ")
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat("and r.RegionID = '{0}' ", regionID)

        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        If Me.txtParent.Text <> "" Then
            sql.AppendFormat("and r.Parent like '%{0}%' ", Me.txtParent.Text.Replace("'", "''"))
        End If

        If Me.txtParentName.Text <> "" Then
            sql.AppendFormat("and r_parent.RegionName like '%{0}%' ", Me.txtParentName.Text.Replace("'", "''"))
        End If

        Return sql.ToString()
    End Function

    Private Function DelData(ByVal rowIndex As Integer) As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim key As DataKey = Me.gv.DataKeys(rowIndex)

        Using scope As New Transactions.TransactionScope

            sql.Length = 0
            sql.AppendFormat("delete Region ")
            sql.AppendFormat(" where RegionID = '{0}' ", key("RegionID"))

            dat.AccData(sql)

            scope.Complete()
        End Using

        Return True
    End Function

    Public Property SelectedRegionID() As String
        Get
            Return ViewState("SelectedRegionID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedRegionID") = value
        End Set
    End Property

    Public Property DefaultParentID() As String
        Get
            Return ViewState("DefaultParentID")
        End Get
        Set(ByVal value As String)
            ViewState("DefaultParentID") = value
        End Set
    End Property

    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)
    Public Event Adding(ByVal source As Object, ByVal e As EventArgs)
    Public Event Updating(ByVal source As Object, ByVal e As EventArgs)
End Class